<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		Broadcastweb.eu - Paules												/
/	Site:			http://www.broadcastweb.eu												/
/	Email:			support@broadcastweb.eu													/
/	Copyright:		2011 - 2012 (c) Broadcastweb.eu											/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0003																	/
/																							/
/========================================== JooSam ========================================*/
// Joomla security
defined('_JEXEC') or die;
jimport('joomla.application.component.controllerform');
jimport('joomla.database.table');
 
// Controller Reqevt view
class JooSamControllerReqevt extends JControllerForm{
	protected $text_prefix = 'Reqevt';

	public function save($key = null, $urlVar = null){

		$data = JRequest::getVar('jform', array(), 'post', 'array');
		if ($data['id'] == '0'){
			$addEvtReq = $this->addEvtReq();
			if($addEvtReq){
				$this->setMessage(JText::_('COM_JOOSAM_REQEVT_CONT_CREATE'));
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=reqevts', false));
			}
			else{
				$this->setMessage(JText::_('COM_JOOSAM_REQEVT_CONT_ERR_CREATE'), 'warning');
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=reqevts', false));
			}
		}
		else{
			$editEvtReq = $this->editEvtReq();
			if($editEvtReq){
				$this->setMessage(JText::_('COM_JOOSAM_REQEVT_CONT_EDIT'));
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=reqevts', false));
			}
			else{
				$this->setMessage(JText::_('COM_JOOSAM_REQEVT_CONT_ERR_EDIT'), 'warning');
				$this->setRedirect(JRoute::_('index.php?option=com_joosam&view=reqevts', false));
			}
		}
	}

	protected function addEvtReq(){

		$data = JRequest::getVar('jform', array(), 'post', 'array');
		$servers = JoosamHelper::currSrvEdit();
		$stype = '';
		foreach($data['view_songtypes'] as $st){
			$stype = $stype.','.$st;
		}
		$songtype = substr($stype, 1);
		$srvid = $data['srv_id'];
		if(!$srvid){ $srvid = $servers['id']; }
		$day0 = $data['evtday'][0]; $day1 = $data['evtday'][1]; $day2 = $data['evtday'][2]; $day3 = $data['evtday'][3]; $day4 = $data['evtday'][4]; $day5 = $data['evtday'][5]; $day6 = $data['evtday'][6];
		$evt_sunday = 0; $evt_saterday = 0; $evt_friday = 0; $evt_thursday = 0; $evt_wednesday = 0; $evt_tuesday = 0; $evt_monday = 0; 
		if($day0 == 'SUN' OR $day1 == 'SUN' OR $day2 == 'SUN' OR $day3 == 'SUN' OR $day4 == 'SUN' OR $day5 == 'SUN' OR $day6 == 'SUN'){$evt_sunday = 1;}
		if($day0 == 'SAT' OR $day1 == 'SAT' OR $day2 == 'SAT' OR $day3 == 'SAT' OR $day4 == 'SAT' OR $day5 == 'SAT' OR $day6 == 'SAT'){$evt_saterday = 1;}
		if($day0 == 'FRI' OR $day1 == 'FRI' OR $day2 == 'FRI' OR $day3 == 'FRI' OR $day4 == 'FRI' OR $day5 == 'FRI' OR $day6 == 'FRI'){$evt_friday = 1;}
		if($day0 == 'THU' OR $day1 == 'THU' OR $day2 == 'THU' OR $day3 == 'THU' OR $day4 == 'THU' OR $day5 == 'THU' OR $day6 == 'THU'){$evt_thursday = 1;}
		if($day0 == 'WED' OR $day1 == 'WED' OR $day2 == 'WED' OR $day3 == 'WED' OR $day4 == 'WED' OR $day5 == 'WED' OR $day6 == 'WED'){$evt_wednesday = 1;}
		if($day0 == 'TUE' OR $day1 == 'TUE' OR $day2 == 'TUE' OR $day3 == 'TUE' OR $day4 == 'TUE' OR $day5 == 'TUE' OR $day6 == 'TUE'){$evt_tuesday = 1;}
		if($day0 == 'MON' OR $day1 == 'MON' OR $day2 == 'MON' OR $day3 == 'MON' OR $day4 == 'MON' OR $day5 == 'MON' OR $day6 == 'MON'){$evt_monday = 1;}

		// Send to sql
		$id = $data['id'];
		$srv_id = $srvid;
		$evt_time_start = $data['evt_time_start'];
		$evt_time_end = $data['evt_time_end'];
		$evt_day_mon = $evt_monday;
		$evt_day_tue = $evt_tuesday;
		$evt_day_wed = $evt_wednesday;
		$evt_day_thu = $evt_thursday;
		$evt_day_fri = $evt_friday;
		$evt_day_sat = $evt_saterday;
		$evt_day_sun = $evt_sunday;
		$evt_date = $data['evt_date'];
		$evt_output_title = $data['evt_output_title'];
		$evt_output_content = $data['evt_output_content'];
		$evt_name = $data['evt_name'];
		$evt_description = $data['evt_description'];
		$auto_update = $data['auto_update'];
		$view_songtype = $songtype;
		$view_categories = $data['view_categories'];
		$accapt_categories = $data['accapt_categories'];
		$front_req_text = $data['front_req_text'];
		$request_msg = $data['request_msg'];
		$more_info_html = $data['more_info_html'];
		$db =& JFactory::getDBO();
		$query = "
				INSERT INTO #__joosam_evtreq (
					srv_id,
					evt_time_start,
					evt_time_end,
					evt_day_mon,
					evt_day_tue,
					evt_day_wed,
					evt_day_thu,
					evt_day_fri,
					evt_day_sat,
					evt_day_sun,
					evt_date,
					evt_output_title,
					evt_output_content,
					evt_name,
					evt_description,
					auto_update,
					view_songtype,
					view_categories,
					accapt_categories,
					front_req_text,
					request_msg,
					more_info_html
				)
				VALUES (
					".$db->quote($srv_id).",
					".$db->quote($evt_time_start).",
					".$db->quote($evt_time_end).",
					".$db->quote($evt_day_mon).",
					".$db->quote($evt_day_tue).",
					".$db->quote($evt_day_wed).",
					".$db->quote($evt_day_thu).",
					".$db->quote($evt_day_fri).",
					".$db->quote($evt_day_sat).",
					".$db->quote($evt_day_sun).",
					".$db->quote($evt_date).",
					".$db->quote($evt_output_title).",
					".$db->quote($evt_output_content).",
					".$db->quote($evt_name).",
					".$db->quote($evt_description).",
					".$db->quote($auto_update).",
					".$db->quote($view_songtype).",
					".$db->quote($view_categories).",
					".$db->quote($accapt_categories).",
					".$db->quote($front_req_text).",
					".$db->quote($request_msg).",
					".$db->quote($more_info_html)."
				)";
		$db->setQuery($query);
		$result = $db->query();
		return $result;
	}

	protected function editEvtReq(){

		$data = JRequest::getVar('jform', array(), 'post', 'array');
		$servers = JoosamHelper::currSrvEdit();
		$stype = '';
		foreach($data['view_songtypes'] as $st){
			$stype = $stype.','.$st;
		}
		$songtype = substr($stype, 1);
		$srvid = $data['srv_id'];
		if(!$srvid){ $srvid = $servers['id']; }
		$day0 = $data['evtday'][0]; $day1 = $data['evtday'][1]; $day2 = $data['evtday'][2]; $day3 = $data['evtday'][3]; $day4 = $data['evtday'][4]; $day5 = $data['evtday'][5]; $day6 = $data['evtday'][6];
		$evt_sunday = 0; $evt_saterday = 0; $evt_friday = 0; $evt_thursday = 0; $evt_wednesday = 0; $evt_tuesday = 0; $evt_monday = 0; 
		if($day0 == 'SUN' OR $day1 == 'SUN' OR $day2 == 'SUN' OR $day3 == 'SUN' OR $day4 == 'SUN' OR $day5 == 'SUN' OR $day6 == 'SUN'){$evt_sunday = 1;}
		if($day0 == 'SAT' OR $day1 == 'SAT' OR $day2 == 'SAT' OR $day3 == 'SAT' OR $day4 == 'SAT' OR $day5 == 'SAT' OR $day6 == 'SAT'){$evt_saterday = 1;}
		if($day0 == 'FRI' OR $day1 == 'FRI' OR $day2 == 'FRI' OR $day3 == 'FRI' OR $day4 == 'FRI' OR $day5 == 'FRI' OR $day6 == 'FRI'){$evt_friday = 1;}
		if($day0 == 'THU' OR $day1 == 'THU' OR $day2 == 'THU' OR $day3 == 'THU' OR $day4 == 'THU' OR $day5 == 'THU' OR $day6 == 'THU'){$evt_thursday = 1;}
		if($day0 == 'WED' OR $day1 == 'WED' OR $day2 == 'WED' OR $day3 == 'WED' OR $day4 == 'WED' OR $day5 == 'WED' OR $day6 == 'WED'){$evt_wednesday = 1;}
		if($day0 == 'TUE' OR $day1 == 'TUE' OR $day2 == 'TUE' OR $day3 == 'TUE' OR $day4 == 'TUE' OR $day5 == 'TUE' OR $day6 == 'TUE'){$evt_tuesday = 1;}
		if($day0 == 'MON' OR $day1 == 'MON' OR $day2 == 'MON' OR $day3 == 'MON' OR $day4 == 'MON' OR $day5 == 'MON' OR $day6 == 'MON'){$evt_monday = 1;}

		// Send to sql
		$id = $data['id'];
		$srv_id = $srvid;
		$evt_time_start = $data['evt_time_start'];
		$evt_time_end = $data['evt_time_end'];
		$evt_day_mon = $evt_monday;
		$evt_day_tue = $evt_tuesday;
		$evt_day_wed = $evt_wednesday;
		$evt_day_thu = $evt_thursday;
		$evt_day_fri = $evt_friday;
		$evt_day_sat = $evt_saterday;
		$evt_day_sun = $evt_sunday;
		$evt_date = $data['evt_date'];
		$evt_output_title = $data['evt_output_title'];
		$evt_output_content = $data['evt_output_content'];
		$evt_name = $data['evt_name'];
		$evt_description = $data['evt_description'];
		$auto_update = $data['auto_update'];
		$view_songtype = $songtype;
		$view_categories = $data['view_categories'];
		$accapt_categories = $data['accapt_categories'];
		$front_req_text = $data['front_req_text'];
		$request_msg = $data['request_msg'];
		$more_info_html = $data['more_info_html'];

		$db =& JFactory::getDBO();
			$query = "
				UPDATE ".$db->nameQuote('#__joosam_evtreq')."
				SET ".$db->nameQuote('evt_time_start')." = ".$db->quote($evt_time_start).",
					".$db->nameQuote('evt_time_end')." = ".$db->quote($evt_time_end).",
					".$db->nameQuote('evt_day_mon')." = ".$db->quote($evt_day_mon).",
					".$db->nameQuote('evt_day_tue')." = ".$db->quote($evt_day_tue).",
					".$db->nameQuote('evt_day_wed')." = ".$db->quote($evt_day_wed).",
					".$db->nameQuote('evt_day_thu')." = ".$db->quote($evt_day_thu).",
					".$db->nameQuote('evt_day_fri')." = ".$db->quote($evt_day_fri).",
					".$db->nameQuote('evt_day_sat')." = ".$db->quote($evt_day_sat).",
					".$db->nameQuote('evt_day_sun')." = ".$db->quote($evt_day_sun).",
					".$db->nameQuote('evt_date')." = ".$db->quote($evt_date).",
					".$db->nameQuote('evt_output_title')." = ".$db->quote($evt_output_title).",
					".$db->nameQuote('evt_output_content')." = ".$db->quote($evt_output_content).",
					".$db->nameQuote('evt_name')." = ".$db->quote($evt_name).",
					".$db->nameQuote('evt_description')." = ".$db->quote($evt_description).",
					".$db->nameQuote('auto_update')." = ".$db->quote($auto_update).",
					".$db->nameQuote('view_songtype')." = ".$db->quote($view_songtype).",
					".$db->nameQuote('view_categories')." = ".$db->quote($view_categories).",
					".$db->nameQuote('accapt_categories')." = ".$db->quote($accapt_categories).",
					".$db->nameQuote('front_req_text')." = ".$db->quote($front_req_text).",
					".$db->nameQuote('request_msg')." = ".$db->quote($request_msg).",
					".$db->nameQuote('more_info_html')." = ".$db->quote($more_info_html)."
				WHERE ".$db->nameQuote('id')." = ".$db->quote($id).";
			";
		$db->setQuery($query);
		$result = $db->query();
		return $result;
	}
}